package fr.upmc.cie.data;

import java.util.ArrayList;
import java.util.HashMap;

import fr.upmc.cie.data.exception.TicketNotFoundException;
import fr.upmc.cie.data.interfaces.ITicketDataSource;

public class TicketManager {

	private HashMap<Long,ArrayList<TicketItem>> tickets = new HashMap<Long, ArrayList<TicketItem>>();
	private ITicketDataSource dataSource;
	private ArrayList<TicketItem> archivedTickets;
	
	protected TicketManager (ITicketDataSource dataSource){
		this.dataSource = dataSource;
		archivedTickets = dataSource.getAllTicket();
	}
	
	/**
	 * Supprimer tous les tickets
	 */
	public void clear(){
		this.tickets.clear();
	}

	/**
	 * Renvoie tous les tickets
	 * @return
	 */
	public HashMap<Long,ArrayList<TicketItem>> getTickets() {
		return tickets;
	}
	
	/**
	 * Renvoie une liste des tickets a partir d'id de channel
	 * @param id
	 * @return
	 */
	public ArrayList<TicketItem> getListById(Long id){
		if (!tickets.containsKey(id)) return null;			
		return tickets.get(id);
	}
	
	/**
	 * Ajouter une liste des tickets
	 * @param channel
	 * @param tickets
	 */
	public void addNewTickets(ChannelItem channel, ArrayList<TicketItem> tickets){
		this.tickets.put(channel.getId(), tickets);
	}
	
	/**
	 * Arichiver un ticket
	 * @param ti
	 */
	public void archiverTicket(TicketItem ti){
		dataSource.insert(ti);
	}
	
	/**
	 * Desarchiver un ticket
	 * @param ti
	 * @return
	 */
	public boolean unarchiverTicket(TicketItem ti){
		try {
			dataSource.delete(ti);
			return true;
		} catch (TicketNotFoundException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * Mettre a jour un ticket
	 * @param ti
	 * @return
	 */
	public boolean updateTicket(TicketItem ti){
		if (dataSource.isTicketArchived(ti)!=0){
			try {
				dataSource.update(ti);
				return true;
			} catch (TicketNotFoundException e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}
	
	/**
	 * Tester si un ticket est archive dans la base
	 * @param ti
	 * @return
	 */
	public boolean isTicketArchived(TicketItem ti){
		return dataSource.isTicketArchived(ti)!=0;
	}
	
	/**
	 * Renvoie tous les tickets archives
	 * @return
	 */
	public ArrayList<TicketItem> getAllArchivedTickets(){
		return this.archivedTickets;
	}
	
	/**
	 * Chercher le ticket suivant
	 * @param ti
	 * @return
	 */
	public TicketItem getNextTicket(TicketItem ti){
		for (Long key : tickets.keySet()){
			for (TicketItem tmp : tickets.get(key))
				if (tmp.equals(ti)){
					int position = tickets.get(key).indexOf(tmp);
					if (position<tickets.get(key).size()-1)
						return tickets.get(key).get(position+1);
				}
		}
		return null;
	}
	
	/**
	 * Chercher le ticket precedent
	 * @param ti
	 * @return
	 */
	public TicketItem getPreviousTicket(TicketItem ti){
		for (Long key : tickets.keySet()){
			for (TicketItem tmp : tickets.get(key))
				if (tmp.equals(ti)){
					int position = tickets.get(key).indexOf(tmp);
					if (position>0)
						return tickets.get(key).get(position-1);
				}
		}
		return null;
	}

	
	/**
	 * Obtenir le rating d'un ticker
	 * @param ti
	 * @return
	 */
	public float getRating(TicketItem ti) {
		long id = dataSource.isTicketArchived(ti);
		if (id>0)
			try {
				return dataSource.getTicketById(id).getRating();
			} catch (TicketNotFoundException e) {}
		return 0;
	}
}
